/**
 * @author 雷明浩
 * @date 2025/6/3 15:49
 */
public class DeleteDuplicates2 {
    // newcoder 删除有序链表中重复的元素-II
    // https://www.nowcoder.com/practice/71cef9f8b5564579bf7ed93fbe0b2024?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj%3FquestionJobId%3D10%26subTabName%3Donline_coding_page
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        ListNode phead = new ListNode(-1001);
        ListNode prev = null;
        ListNode cur1 = phead;
        ListNode cur2 = head;
        while (cur2 != null) {
            if (cur2.val != cur1.val) {
                prev = cur1;
                cur1.next = cur2;
                cur1 = cur1.next;
                cur2 = cur2.next;
            }else {
                int tmp = cur2.val;
                while (cur2 != null && cur2.val == tmp) {
                    cur2 = cur2.next;
                }
                prev.next = null;
                cur1 = prev;
            }
        }
        return phead.next;
    }

    public static void main(String[] args) {
        ListNode head = new ListNode(1);
        head.next = new ListNode(1);
        head.next.next = new ListNode(2);
        DeleteDuplicates2 del = new DeleteDuplicates2();
        head = del.deleteDuplicates(head);
    }
}